home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Car / Engine.m < prev    next >
Encoding:
Text File  |  1992-06-23  |  3.6 KB  |  149 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Car_main.h"
  5. #import "Engine.h"
  6. #import "DataView.h"
  7. #import "defs.h"
  8. #import "appkit/nextstd.h"
  9. #import "Transmission.h"
  10. #import "DataView.h"
  11. #import "Cycle.h"
  12. #import "GasTank.h"
  13.  
  14. @implementation Engine
  15.  
  16. - init
  17. {
  18.     [super init];
  19.     engine = self;
  20.     return self;
  21. }
  22.  
  23. - read:(NXTypedStream *)stream
  24. {
  25.     [super read:stream];
  26.     NXReadTypes(stream,"fffff",&efficiency,&mass,&redline,&stallSpeed,&startLag);
  27.     NXReadArray(stream,"f",6,coefficients);
  28.     return self;
  29. }
  30.  
  31. - write:(NXTypedStream *)stream
  32. {
  33.     [super write:stream];
  34.     NXWriteTypes(stream,"fffff",&efficiency,&mass,&redline,&stallSpeed,&startLag);
  35.     NXWriteArray(stream,"f",6,coefficients);
  36.     return self;
  37. }
  38.  
  39. - (float *)coefficients
  40. {
  41.     return coefficients;
  42. }
  43.  
  44. - setCoefficients:(float *)theCoefficients
  45. {
  46. int i;
  47.  
  48.     for ( i = 0 ; i < 6 ; i++ )
  49.         coefficients[i] = theCoefficients[i];
  50.     return self;
  51. }
  52.  
  53. - (float)efficiency
  54. {
  55.     return efficiency;
  56. }
  57.  
  58. - setEfficiency:(float)aNumber
  59. {
  60.     efficiency = aNumber;
  61.     return self;
  62. }
  63.  
  64. - (float)mass
  65. {
  66.     return mass;
  67. }
  68.  
  69. - setMass:(float)aNumber
  70. {
  71.     mass = aNumber;
  72.     return self;
  73. }
  74.  
  75. - (float)redline
  76. {
  77.     return redline;
  78. }
  79.  
  80. - setRedline:(float)aNumber
  81. {
  82.     redline = aNumber;
  83.     return self;
  84. }
  85.  
  86. - (float)stallSpeed
  87. {
  88.     return stallSpeed;
  89. }
  90.  
  91. - setStallSpeed:(float)aNumber
  92. {
  93.     stallSpeed = aNumber;
  94.     return self;
  95. }
  96.  
  97. - (float)startLag
  98. {
  99.     return startLag;
  100. }
  101.  
  102. - setStartLag:(float)aNumber
  103. {
  104.     startLag = aNumber;
  105.     return self;
  106. }
  107.  
  108. /******************************************************************************************************************************
  109.  *    This is the part that is called during the simulation.  Notice that the engine will deliver as much stopping power as   *
  110.  *    needed.                                                                                                                 *
  111.  ******************************************************************************************************************************/
  112. - powerRequired:(float)power
  113. {
  114. float angVelocity;
  115. float torqueRequired;
  116. float torqueAvailable;
  117. float energyRequired;
  118.  
  119.     angVelocity = [transmission inputSpeed];
  120.     if ( angVelocity != 0 )
  121.        torqueRequired = power / angVelocity;
  122.     else if ( power > 0 )
  123.        torqueRequired = 999999;
  124.     else if ( power < 0 )
  125.        torqueRequired = -999999;
  126.     else if ( power == 0 )
  127.        torqueRequired = 0;
  128.     torqueAvailable = evaluatePolynomial(coefficients,6,angVelocity * 60 / (2 * PI)); // Remember the map is in rpm
  129.  
  130.     if ( torqueRequired >= 0 )                        // Here is where we actually pass the power on.
  131.         [transmission engineInput:MIN(torqueAvailable,torqueRequired)];    // Make sure we don't give more power than is needed.
  132.     else
  133.         [transmission engineInput:torqueRequired];            // The car can have all the negative power it wants.
  134.  
  135. /******************************************************************************************************************************
  136.  *    Here's where we update the gas tank.  There are no provisions to stop the simulation if we run out of gas, but the      *
  137.  *    final gas level will turn out negative.  This is not nearly as important as the whole battery/charge thing.             *
  138.  ******************************************************************************************************************************/
  139.     if ( torqueRequired > 0 )        // Only draw gas if we require positive power.
  140.         {
  141.         energyRequired = MIN(torqueAvailable,torqueRequired) * angVelocity * [cycle timeStep] / efficiency;
  142.         [gasTank energyRequired:energyRequired];
  143.         }
  144.  
  145.     return self;
  146. }
  147.  
  148. @end
  149.